package com.hulk.dryad.admin.shedlock;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.text.StrFormatter;
import com.hulk.dryad.manage.framework.websocket.client.core.WsClient;
import com.hulk.dryad.manage.framework.websocket.common.message.WsMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author: hulk
 * @E-mail: 29572320@qq.com
 * @Date: Create in 2019-05-18 22:37
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RestReminderKeepingScheduler {

    private static final String DESTINATION = "/topic/restReminder";

	private final WsClient wsClient;


	@Scheduled(cron = "0 */5 * * * ?")
	@SchedulerLock(name = "socketRestReminder", lockAtLeastFor = "PT30S", lockAtMostFor = "PT30M")
	public void socketRestRemind() {
		String time = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.NORM_TIME_PATTERN);
		String payload = StrFormatter.format("现在时间是: [{}]，您连续工作时间过长，请注意休息！", time);
		log.info(payload);
		WsMessage ws = WsMessage.builder()
				.user("") // 用户，非必须
				.tenantId("")//租户，非必须
				.destination(DESTINATION) // 前端的 destination
				.payload(payload) // 消息内容
				.build();
		wsClient.send(ws);
	}
}